package com.niit.dao.impl;

import java.util.List;

import com.niit.bean.Admin;
import com.niit.dao.AdminDao;
import com.niit.utils.DbUtil;

public class AdminDaoImpl implements AdminDao {
	
	private DbUtil util = new DbUtil();

	@Override
	public int add(Admin t) {
		String sql = "insert into tb_admin (username,password) values (?,?)";
		int a = util.update(sql, t.getUsername(),t.getPassword());
		return a;
	}

	@Override
	public int delete(Admin t) {
		String sql = "delete tb_admin where username = ?";
		int a = util.update(sql, t.getUsername());
		return a;
	}

	@Override
	public int update(Admin t) {
		String sql = "update tb_admin set username = ?,password = ?,permission = ?";
		int a = util.update(sql, t.getUsername(),t.getPassword(),t.getPermission());
		return a;
	}

	@Override
	public List<Admin> findAll(int start, int end) {
		String sql = "select * from (select rownum r,tb_admin.* from tb_admin) a where a.r>? and a.r<=?";
		List<Admin> list = util.select(sql, Admin.class, start,end);
		return list;
	}

	@Override
	public int findTotal() {
		String sql = "select count(*) from tb_admin";
		return util.selectCount(sql);
	}

	@Override
	public boolean usernameExist(String username) {
		String sql = "select * from tb_admin where username = ?";
		List<Admin> list = util.select(sql, Admin.class, username);
		return list==null?false:true;
	}

	@Override
	public boolean login(Admin admin) {
		String sql = "select count(*) from tb_admin where username = ? and password = ?";
		int a = util.selectCount(sql, admin.getUsername(),admin.getPassword());
		return a>0?true:false;
	}
}
